package com.hzdesk.media.koreabatchmatvector.utils;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static android.content.Context.WIFI_SERVICE;

/**
 * Created by Administrator on 2018/8/6.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class HotUtils {
    /**
     * @desc 打开热点
     * */
    public static boolean  startWifiAp(String ssid, String passwd,Context context,boolean enabled) {
        WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            //wifi和热点不能同时打开，所以打开热点的时候需要关闭wifi
            if (enabled) {
                mWifiManager.setWifiEnabled(false);
            }
        try {
            // 热点的配置类
            WifiConfiguration apConfig = new WifiConfiguration();
            // 配置热点的名称(可以在名字后面加点随机数什么的)
            apConfig.SSID = ssid;
            apConfig.preSharedKey = passwd;
            apConfig.allowedKeyManagement.set(4);//设置加密类型，这里4是wpa加密
            Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
            // 返回热点打开状态
            return (Boolean) method.invoke(mWifiManager, apConfig, enabled);
        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }
    }



    public static WifiConfiguration getWifiConfiguration(Context context){
            WifiConfiguration mWifiConfig = null;
            try {
                WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
                Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
                method.setAccessible(true);
                mWifiConfig = (WifiConfiguration) method.invoke(wifiManager);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return mWifiConfig;
    }
    public static String getApSSIDAndPwd(Context context, int flag){
        WifiConfiguration mWifiConfig = getWifiConfiguration(context);
        String ssid = null;
        String pwd = null;
        if (null != mWifiConfig) {
            Field[] fields = mWifiConfig.getClass().getDeclaredFields();
            if (null != fields) {
                for (Field field : fields) {
                    try {
                        if (field.getName().equals("SSID")) {
                            ssid = field.get(mWifiConfig).toString();
                            Log.e("HotUtils", "AP SSI = " + ssid);
                        } else if (field.getName().equals("preSharedKey")) {
                            pwd = field.get(mWifiConfig).toString();
                            Log.e("HotUtils", "AP pwd = " + pwd);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("HotUtils", "getApSSIDAndPwd()-->error:" + e);
                    }
                }
            }
        }


        if (null == ssid) {
            ssid = "Unknown";
        }
        if (null == pwd) {
            pwd = "Unknown";
        }
        if (0 == flag) {
            return ssid;
        } else {
            return pwd;
        }

    }
    /**
     * 热点开关是否打开
     * @return
     */
    public static boolean isWifiApEnabled(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
        try {
            Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
            method.setAccessible(true);
            return (Boolean) method.invoke(wifiManager);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 关闭WiFi热点
     */
    public static void closeWifiAp(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (isWifiApEnabled(context)) {
            try {
                Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
                method.setAccessible(true);
                WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager);
                Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                method2.invoke(wifiManager, config, false);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}
